import { Response, ServiceConfig, route, Guid, Logger, RequestLog } from '../common';
import { CloudSyncService } from '../service/cloudSyncService';
import { FrontStoreStockService } from '../service/frontStoreStockService';
import { ProduceRecordService } from '../service/produceRecordService';
import { KitchensolutionService } from '../service/kitchensolutionService';
import { ProduceRecord, } from '../domain/model';
import { WebException } from './webException';
import { RequestDataVerify } from '../common/http/router/routeDecorators';
import { DataVerification } from './verification/baseVerification';
import { FNS } from '../common/fnc/fncManager';

export class ScrapOutFrontStoreStockController {

    private syncSvr: CloudSyncService;
    private frontStoreStockSvr: FrontStoreStockService;
    private produceRecordSrc: ProduceRecordService
    private kitchensolutionSvc: KitchensolutionService
    constructor() {
        this.syncSvr = ServiceConfig.Current.GetService<CloudSyncService>("cloudSyncService");
        this.frontStoreStockSvr = ServiceConfig.Current.GetService<FrontStoreStockService>("frontStoreStockService");
        this.produceRecordSrc = ServiceConfig.Current.GetService<ProduceRecordService>("produceRecordService");
        this.kitchensolutionSvc = ServiceConfig.Current.GetService<KitchensolutionService>("kitchensolutionService");
    }

    toString() {
        return 'ScrapOutFrontStoreStockController';
    }

    /**
     * 获取报废历史
     * 
     * @param {any} ctx 
     * @param {any} next 
     * 
     * @memberof ScrapOutFrontStoreStockController
     */
    @route({ method: 'get', path: '/api/scrapOutFrontStoreStocks/getScrapOutStocks' })
    async getScrapOutStocks(ctx, next) {
        let startTime = ctx.query.startTime;
        let endTime = ctx.query.endTime;
        let storeId = ctx.accept.headers["storeid"];
        let scrapOutStocks = await this.frontStoreStockSvr
            .GetScrapOutList(storeId, startTime, endTime);
        ctx.response.body = Response.ok("获取报废历史成功", scrapOutStocks);
        await next();
    }

    /**
     * 门店前端商品报废
     * 
     * @param {any} ctx 
     * @param {any} next 
     * 
     * @memberof ScrapOutFrontStoreStockController
     */
    @route({ method: 'post', path: '/api/scrapOutFrontStoreStocks/setScrapOut' })
    @RequestLog()
    @RequestDataVerify(new DataVerification.ScrapOutFrontStoreStocks())
    async setScrapOut(ctx, next) {
        let creator = ctx.request.body.creator;
        let terminal = ctx.request.body.terminal;
        let frontStoreStocks = ctx.request.body.frontStoreStocks;
        let storeId = ctx.accept.headers["storeid"];
        let updatedStock = await this.frontStoreStockSvr
            .SetScrapOut({
                storeId: storeId,
                creator: creator,
                frontStoreStocks: frontStoreStocks
            });

        if (updatedStock) {
            let soldOutMessage = [];
            for (let i = 0; i < updatedStock.frontStoreStocks.length; i++) {
                let fss = updatedStock.frontStoreStocks[i];
                soldOutMessage.push({
                    id: fss.id,
                    isSoldOut: fss.isSoldOut,
                    soldOutMode: fss.soldOutMode,
                    quantity: fss.quantity
                });
            }
            FNS.FNCManager.Current.Notify(new FNS.SoldOutMessage(soldOutMessage));
        }
        ctx.response.body = Response.ok("报废商品成功", updatedStock);

        // 上传作废纪录
        let syncDataList = []
        let uploadObj = Object.assign({}, updatedStock);
        delete uploadObj.frontStoreStocks;
        syncDataList.push({
            id: uploadObj.id,
            body: uploadObj,
            entityName: "storageReceipts"
        });

        this.syncSvr.DomainDataSyncPackage(
            syncDataList,
            {
                storeId: storeId,
                terminal: terminal,
                user: creator
            }
        );
        await next();
    }
}